package com.sgb.kjwl.view.ui.adapter.pick;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.pick.EnterpriseBiddingDetailEntity5;
import com.sgb.kjwl.model.entity.pick.QuotationListEntity3;
import com.swgk.core.util.UiUtil;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class QuotationListAdapter5 extends BaseRecycleAdapter<EnterpriseBiddingDetailEntity5.GoodsEntity> {

    private EditText et_price;
    private EditText et_ask_describe;
    private TextView tv_num;
    private TextView tv_price;
    private RecyclerView recyclerView;
    private List<EnterpriseBiddingDetailEntity5.GoodsEntity> allList = new ArrayList<>();
    private ISumPrice iSumPrice;
    private String type = "";
    private boolean isShowRed = false;
    private String categoryModel = "";

    public QuotationListAdapter5(Context context, List<EnterpriseBiddingDetailEntity5.GoodsEntity> datas) {
        super(context, R.layout.item_list_of_quotations5, datas);
    }

    @Override
    public void convert(ItemViewHolder holder, final EnterpriseBiddingDetailEntity5.GoodsEntity entity) {
        holder.setBinding(BR.viewModel, entity);
        et_price = holder.itemView.findViewById(R.id.et_price);
        tv_price = holder.itemView.findViewById(R.id.tv_price);
        et_ask_describe = holder.itemView.findViewById(R.id.et_ask_describe);
        tv_num = holder.itemView.findViewById(R.id.tv_num);
        recyclerView = holder.itemView.findViewById(R.id.item_view);

        allList.add(entity);

        //专业工程4   劳务分包2
        if("ja".equals(categoryModel)){
            if (type.equals("14") || type.equals("15") || type.equals("11") || type.equals("19")) {
                tv_price.setText("报价(元)");
            } else {
                tv_price.setText("单价(元)");
            }
        }else{
            if (type.equals("4") || type.equals("2")) {
                tv_price.setText("总价(元)");
            } else if (type.equals("12")) {
                tv_price.setText("报价（元/辆）");
            } else {
                tv_price.setText("单价(元)");
            }
        }

        recyclerView.setLayoutManager(new GridLayoutManager(context, 1));
        List<QuotationListEntity3> list = new ArrayList<>();
        for (String test : entity.getPropertys().subList(0, entity.getPropertys().size())) {
            QuotationListEntity3 quotationListEntity3 = new QuotationListEntity3();
            quotationListEntity3.setName(test);
            list.add(quotationListEntity3);
        }
        //清单文件
//        if (!TextUtils.isEmpty(entity.getFile())){
//            QuotationListEntity3 quotationListEntity2 = new QuotationListEntity3();
//            quotationListEntity2.setKey("filePath");
//            quotationListEntity2.setValue(entity.getFile());
//            list.add(quotationListEntity2);
//        }

        QuotationItemAdapter5 adapter5 = new QuotationItemAdapter5(context, list);
        recyclerView.setAdapter(adapter5);

        if (!"0".equals(entity.getPrice()) && !TextUtils.isEmpty(entity.getPrice())) {
            if (entity.getPrice().contains("e") || entity.getPrice().contains("E")) {
                BigDecimal bd = new BigDecimal(entity.getPrice());
                et_price.setText(bd.toPlainString());
            } else {
                et_price.setText(entity.getPrice() + "");
            }
        }

        if(isShowRed){
            if("0".equals(entity.getPrice()) || TextUtils.isEmpty(entity.getPrice())){
                tv_price.setTextColor(context.getResources().getColor(R.color.color_shopping_car_price));
                et_price.setText("");
            }else{
                tv_price.setTextColor(context.getResources().getColor(R.color.color_tab_one));
            }
        }

        et_price.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void afterTextChanged(Editable editable) {
                String num = TextUtils.isEmpty(editable.toString())?"0":editable.toString();
                if (num.endsWith(".")){
                    num = num + "00";
                }
                allList.get(holder.getAdapterPosition()).setPrice(num);
                iSumPrice.onSum();
            }
        });
        UiUtil.getInputFilter(et_price,4);

        et_ask_describe.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void afterTextChanged(Editable editable) {
                String num = TextUtils.isEmpty(editable.toString())?"":editable.toString();
                allList.get(holder.getAdapterPosition()).setRemark(num);
                tv_num.setText(editable.toString().length()+"/200");
            }
        });
    }

    public List<EnterpriseBiddingDetailEntity5.GoodsEntity> getChangeData() {
        return allList;
    }

    public interface ISumPrice {
        void onSum();
    }

    public void ISumPrice(ISumPrice iSumPrice) {
        this.iSumPrice = iSumPrice;
    }

    public void setType(String type) {
        this.type = type;
        notifyDataSetChanged();
    }

    public void setCategoryModel(String categoryModel) {
        this.categoryModel = categoryModel;
        notifyDataSetChanged();
    }

    public void setShowRed(boolean isShowRed) {
        this.isShowRed = isShowRed;
        notifyDataSetChanged();
    }
}
